﻿//
// GNS_TopAndTickDivider.cpp
// MobileManageSys
//
// Created by mr.l on 16/1/15.
//
//
#include "GnsClientFrame.h"
#include "GNS_TopAndTickDivider.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"

#include "GNS_Common.h"
#include "GNS_SceneMgr.h"

#include "GnsOften.h"
#include "SystemMgr.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
//extern ISocketMgr * _GameSock;

GNS_TopAndTickDivider::GNS_TopAndTickDivider()
{
    pGNS_TopAndTickDivider=0;
    IDtext=nullptr;
   Rename=nullptr;
    AccountedInfo=nullptr;
    Accounted=nullptr;
    Num=nullptr;
   //ReNameText=nullptr;
	reNameText = nullptr;
    AccountedInfoText=nullptr;
    AccountedText=nullptr;
    numText=nullptr;
    stateText=nullptr;
    goldText=nullptr;
    TopText=nullptr;
    tickText=nullptr;
    pMakeLayer=nullptr;
    StateButton=nullptr;
    StateNum=0;
    InputBg=nullptr;
    IsInput=false;
    pModifyData=nullptr;
    ReaName="";

	_reNameText = nullptr;
}

GNS_TopAndTickDivider::~GNS_TopAndTickDivider()
{
    
}

GNS_TopAndTickDivider* GNS_TopAndTickDivider::createLayer()
{
    auto pGNS_TopAndTickDivider = GNS_TopAndTickDivider::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_TopAndTickDivider);
    return pGNS_TopAndTickDivider;
}

bool GNS_TopAndTickDivider::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    return true;
}
void GNS_TopAndTickDivider::SetNodeName(std::string CsbName)
{
    pGNS_TopAndTickDivider = CSLoader::createNode(CsbName);
    pGNS_TopAndTickDivider->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_TopAndTickDivider);
    addChild(pGNS_TopAndTickDivider,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_TopAndTickDivider);
	GNS_NodeMgr::getInstance()->pushChildNode("GNS_TopAndTickDivider", pGNS_TopAndTickDivider);

    auto pBackBtn = static_cast<Button*>(pGNS_TopAndTickDivider->getChildByName("DetailsBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTickDivider::BtnCallBack, this));
    /**
     *  获取界面数据
     */
    IDtext=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("Text_5"));
   auto Rename=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("Rename"));
    AccountedInfo=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("AccountedInfo"));
    Accounted=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("Accounted"));
    Num=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("num"));
     //ReNameText =GNS_SceneMgr::getInstance()->MycreateEditBox("备注名",Vec2(winsize.width*0.54, winsize.height*0.88), 28, Color3B::BLACK,10, false);
	reNameText = static_cast<TextField*>(pGNS_TopAndTickDivider->getChildByName("TextField_rename"));
	reNameText->setTextHorizontalAlignment(TextHAlignment::CENTER);
	reNameText->setEnabled(false);

	_reNameText = GNS_Handler::createTMEditBoxWithExistFeild(reNameText, pGNS_TopAndTickDivider, 10);
	_reNameText->setEnabled(false);

    if(GNS_PlayerMgr::getInstance()->getUserLevel()==0) //判断登录号, 運營商 ,  1，代理商; 2, 推廣員
    {
        //Rename->setString(""); //该代理备注名
		Rename->setVisible(false);
		//reNameText = nullptr;
		reNameText->setVisible(false);
		_reNameText->setVisible(false);
        AccountedInfo->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("本运营商占比"));
        Accounted->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("该代理及下线占比"));
        Num->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("推广员数量"));
    }
   // if (ReNameText)  pGNS_TopAndTickDivider->addChild(ReNameText,12);
    InputBg = static_cast<ImageView*>(pGNS_TopAndTickDivider->getChildByName("Image_2"));
    InputBg->setVisible(false);
    
    AccountedInfoText=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("AccountedInfoText"));
    AccountedText=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("AccountedText"));
    numText=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("numText"));
    stateText=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("stateText"));
    goldText=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("goldText"));
    TopText=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("TopText"));
    tickText=static_cast<Text*>(pGNS_TopAndTickDivider->getChildByName("tickText"));
    //Button
    StateButton = static_cast<Button*>(pGNS_TopAndTickDivider->getChildByName("StateButton"));
    StateButton->setTag(2);
    StateButton->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTickDivider::BtnCallBack, this));
    
    auto ResetButton = static_cast<Button*>(pGNS_TopAndTickDivider->getChildByName("ResetButton"));
    ResetButton->setTag(3);
    ResetButton->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTickDivider::BtnCallBack, this));
    
    pModifyData = static_cast<Button*>(pGNS_TopAndTickDivider->getChildByName("SetSingleButton"));
    if(GNS_PlayerMgr::getInstance()->getUserLevel()>0)
    {
        pModifyData->setTag(4);
        pModifyData->addTouchEventListener(CC_CALLBACK_2(GNS_TopAndTickDivider::BtnCallBack, this));
    }
    else{
        pModifyData->setVisible(false);
        pModifyData->setEnabled(false);
    }
    pMakeLayer = GNS_MakeLayer::CreateMakeLayer("加载中...");
    this->addChild(pMakeLayer,999);
}
void GNS_TopAndTickDivider::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.erase(1);
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }

				//GNS_NodeMgr::getInstance()->eraseChildNode("GNS_TopAndTickDivider");

                auto PromoterNode=GNS_PlayerMgr::getInstance()->_NodeChildVec.at(0);
                PromoterNode->setVisible(true);

				//GNS_NodeMgr::getInstance()->setChildVisible()
                break;
            }
            case 2:
            {
                if (StateNum==1) {
                    Req_forbid_AgentUser  pUnfor;
                    pUnfor.UserId=(long long)atoi(IDtext->getString().c_str());
                    pUnfor.OperatorUserId=GNS_PlayerMgr::getInstance()->getMyUseridx();
                    //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_REQ_UNFORBID_AGENT_USER, &pUnfor, sizeof(pUnfor));
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_REQ_UNFORBID_AGENT_USER, (char*)&pUnfor, sizeof(pUnfor));
                }else if(StateNum==0){
                    Req_unforbid_AgentUser  pUnfor;
                    pUnfor.UserId=(long long)atoi(IDtext->getString().c_str());
                    pUnfor.OperatorUserId=GNS_PlayerMgr::getInstance()->getMyUseridx();
                    //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_REQ_FORBID_AGENT_USER, &pUnfor, sizeof(pUnfor));
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_REQ_FORBID_AGENT_USER, (char*)&pUnfor, sizeof(pUnfor));
                }
                break;
            }
            case 3:
            {
                CMD_CS_ResetAgentPassword  pResPassword;
                pResPassword.llFatherAgentID=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                pResPassword.llAgentID=atoi(IDtext->getString().c_str());
                //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_USER_RESET_PASSWORD, &pResPassword, sizeof(pResPassword));
               GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_USER_RESET_PASSWORD, (char*)&pResPassword, sizeof(pResPassword));
                break;
            }
            case 4:
            {
                
                if (IsInput==false) {
                    IsInput=true;
                    InputBg->setVisible(true);
                    pModifyData->loadTextureNormal("change_password_finish.png");
                    pModifyData->loadTexturePressed("change_password_finish.png");
					if (_reNameText)
					{
						_reNameText->setEnabled(true);
						//reNameText->setTouchAreaEnabled(true);
					}
						
                }else{

					//check textfield first
					if (!checkInput())
						return;

                    IsInput=false;
                    InputBg->setVisible(false);
                    pModifyData->loadTextureNormal("change_password_button.png");
                    pModifyData->loadTexturePressed("change_password_button.png");
                    CMD_CS_SetSingleAgentInfo pSetSingAg;
                    pSetSingAg._agentCode = atoi(IDtext->getString().c_str());
					string ReaNameTmp = SystemMgr::getInstance()->utf8ToGb(ReaName.c_str());
                    memcpy(pSetSingAg._oldRemarkName,ReaNameTmp.c_str() , 33);
					if (_reNameText)
					{
						string reNameTextTmp = SystemMgr::getInstance()->utf8ToGb(_reNameText->getText());
						memcpy(pSetSingAg._newRemarkName, reNameTextTmp.c_str(), 33);
					}	
                    GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_SET_SINGLE_AGENT_INFO, (char*)&pSetSingAg, sizeof(pSetSingAg));
					if (_reNameText)
						_reNameText->setEnabled(false);
                }
                
            }
            default:
                break;
        }
    }
}
void GNS_TopAndTickDivider::SetDivider(CMD_SC_GetSingleAgentInfoResult* pSingleInfo)
{
    pMakeLayer->Show(false);
    IDtext->setString(StringUtils::format("%d",pSingleInfo->_agentCode));
	if (_reNameText)
		_reNameText->setText(SystemMgr::getInstance()->GbToUTF8(pSingleInfo->_remarkName).c_str());//
    AccountedInfoText->setString(StringUtils::format("%d",pSingleInfo->_agentPercent));
    AccountedText->setString(StringUtils::format("%d",pSingleInfo->_lowlinePercent));
    numText->setString(StringUtils::format("%d",pSingleInfo->_userNum));
    goldText->setString(StringUtils::format("%d",pSingleInfo->_agentGold));
    TopText->setString(StringUtils::format("%d",pSingleInfo->_rechargeVal));
    tickText->setString(StringUtils::format("%d",pSingleInfo->_rewardVal));
    SetButtonState(pSingleInfo->_agentState);
    ReaName=SystemMgr::getInstance()->GbToUTF8(pSingleInfo->_remarkName);
}
void GNS_TopAndTickDivider::SetButtonState(int State)
{
    if (State==1) {
        StateButton->loadTextureNormal("sethonor.png");
        StateButton->loadTexturePressed("sethonor.png");
        std::string Name = CCGnsOften::getUtf8_DirectAssignmentInProgram("正常");
        stateText->setString(Name);
        StateNum=0;
    }else if (State==0){
        StateButton->loadTextureNormal("de-archive.png");
        StateButton->loadTexturePressed("de-archive.png");
        stateText->setString(CCGnsOften::getUtf8_DirectAssignmentInProgram("封号"));
        StateNum=1;
    }

}
void GNS_TopAndTickDivider::SetNewReName(std::string NewReName)
{
  //  ReNameText->setString(NewReName);
}

bool GNS_TopAndTickDivider::checkInput()
{
	//无备注名 运营商
	if (!_reNameText)
	{
		return true;
	}

	string reName = _reNameText->getText();
	if (!CCGnsOften::checkPlayerName(reName))
	{
		OnUIAction("备注名长度有误！", true);
		return false;
	}

	return true;
}

void GNS_TopAndTickDivider::OnUIAction(std::string Text, bool isUTF)
{
	auto pPopWnd = PopWndLayer::createScene("Popwnd.png", "PageClose.png");
	pPopWnd->setClickWndOutsideClose(true);
	pPopWnd->SetTitleWithUTF("提示!");
	if (isUTF)
		pPopWnd->SetContentTextWithUTF(Text, Color3B::BLACK);
	else
		pPopWnd->SetContentTextWithGB(Text, Color3B::BLACK);
}